
package Test;

import java.io.File;

import junit.framework.TestCase;
import svgedit.gui.Frame;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGStylable;

/**
 *  Test case when delete an element and test when undo this execution
 *
 */
public class DeleteElementTest extends TestCase {

	/**
	 *  Get Frame object
	 * */
	private static Frame frame;
	/**
	 *  Choose an element which is under root group directly
	 * */
	private SVGElement selectedElem;
	
	
	/**
	 *   Constructor for insertElement test
	 * */
	public DeleteElementTest(String arg0) {
		super(arg0);
		frame = new Frame();
		File file = new File("src/testfile/test.svg");
        frame.openFile(file);
        selectedElem = getSelectedElement();
	}
	

	/**
	 *   Get an element in this file
	 *   
	 *   @return elem to get the selectedElement
	 * */
	public SVGElement getSelectedElement(){
		for (SVGElement elem: frame.getDocument()){
			if (elem instanceof SVGStylable && elem.getParent() == frame.getDocument().getRootGroup()){
				return elem;
			}
		}
		return null;
	}
	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();	
	}
	
	/**
	 *  Test unit when delete an element
	 * */
	public void testDeleteElementTest(){
		DeleteElement(selectedElem);
	}
	
	/**
	 *  Test unit when undo the delete execution
	 * */
	public void testUndoDeleteElementTest(){
		AddElement(selectedElem);
	}
	
	/**
	 *  Add the element to its original group
	 *  
	 *  @param element the deleted element
	 * */
	public void AddElement(SVGElement element){
		frame.getDocument().getRootGroup().appendChild(element);
		assertNotNull("Undo fails!!",element.getParent());
	}
	
	/**
	 *  Delete the element from the document
	 *  
	 *  @param element the deleted element
	 * */
	public void DeleteElement(SVGElement element){
		frame.getDocument().getRootGroup().removeChild(element);
		assertNull("Insert fails!!",element.getParent());
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

}
